package com.alianzamedica.controllers.patient;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.objectsearch.web.tools.FormatDateTool;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Patient;
import com.alianzamedica.tools.Enviroment;

/**
 * @author Carlos
 * 
 */
public class PatientView extends Action {

	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Enviroment env = Enviroment.getInstance();
		FormatDateTool formatDate = new FormatDateTool();
		request.setAttribute("formatDate", formatDate);
		Document doc = env.getDocument();
		String sId = request.getParameter("id");
		Integer id = null;
		if (sId != null && sId.trim().length() > 0) {
			try {
				id = Integer.valueOf(sId);
			} catch (Exception e) {
			}
		}
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		if (id != null) {
			Patient p = new Patient();
			p.setId(id);
			Patient patient = null;
			Iterator<Patient> iterator = search.searchObjects(p).iterator();
			while (iterator.hasNext()) {
				patient = (Patient) iterator.next();
			}
			if (patient != null) {
				request.setAttribute("patient", patient);
			}
		}
		return mapping.findForward("success");
	}

}
